//
//  HaomalanCell.m
//  SSC
//
//  Created by 红沙尘 on 17/6/18.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "HaomalanCell.h"
#import "WanfaModel.h"

@interface HaomalanCell ()
{
    //UI视图
    __weak IBOutlet UIView *bgView;
    __weak IBOutlet UIView *viewA;
    __weak IBOutlet UIView *viewB;
    __weak IBOutlet UIView *viewC;
    __weak IBOutlet NSLayoutConstraint *lineA;
    
    //数据视图
    __weak IBOutlet UILabel *wanfaLabel;
    __weak IBOutlet UILabel *haomaLabel;
    __weak IBOutlet UILabel *moneyLabel;
    __weak IBOutlet UILabel *numLabel;
    __weak IBOutlet UILabel *zhushuLabel;
    
    NSMutableArray *selectArray;
}

@property (nonatomic, strong) WanfaModel *model;

@end

@implementation HaomalanCell


- (void)awakeFromNib {
    [super awakeFromNib];
    
    lineA.constant = LineHeight;
    
    bgView.layer.borderColor = COLOR_CCCCCC.CGColor;
    bgView.layer.borderWidth = LineHeight;
    
    viewA.layer.borderColor = COLOR_CCCCCC.CGColor;
    viewA.layer.borderWidth = LineHeight;
    viewB.layer.borderColor = viewA.layer.borderColor;
    viewB.layer.borderWidth = viewA.layer.borderWidth;
    viewC.layer.borderColor = viewA.layer.borderColor;
    viewC.layer.borderWidth = viewA.layer.borderWidth;
}

- (void)loadWithModel:(id)model {
    self.model = model;
    
    NSString *name = [self.model.name substringToIndex:self.model.name.length - 1];
    wanfaLabel.text = [NSString stringWithFormat:@"玩法:%@",name];
    
    numLabel.text   = [NSString stringWithFormat:@"%d",self.model.beishu];

    [self updateWillPayMoney];
    
    NSMutableArray *haomaArray = @[].mutableCopy;
    
    for (HaomaSelectModel *selectModel in self.model.dataArray) {

        NSMutableArray *array = [selectModel allHaoma];
        NSMutableArray *wei = @[].mutableCopy;
        for (HaomaModel *haomaModel in array) {
            if (haomaModel.selected) {

                if ([wanfaLabel.text rangeOfString:@"龙虎和>万个"].length > 1) {
                    [wei addObject:@[@"龙",@"虎",@"和"][haomaModel.number]];
                } else {
                    [wei addObject:[NSString stringWithFormat:@"%d",haomaModel.number]];
                }
            }
        }
        
        if (wei.count > 0) {
            [haomaArray addObject:[wei componentsJoinedByString:@" "]];
        } else {
            [haomaArray addObject:@"-"];
        }
        
    }
    
    haomaLabel.text = [haomaArray componentsJoinedByString:@" | "];
}

- (IBAction)jian {
    int num = [numLabel.text intValue];
    
    num --;
    
    num = MAX(num, 1);
    
    numLabel.text = [NSString stringWithFormat:@"%d",num];
    
    [self.model updateBeishu:num];
    
    [self updateWillPayMoney];
}

- (IBAction)jia {
    int num = [numLabel.text intValue];
    
    num ++;
    
    numLabel.text = [NSString stringWithFormat:@"%d",num];

    [self.model updateBeishu:num];
    
    [self updateWillPayMoney];
}


- (void)updateWillPayMoney {
    
    if (!selectArray) selectArray = [[NSMutableArray alloc] init];
    
    [selectArray removeAllObjects];
    
    
    for (HaomaSelectModel *selectModel in self.model.dataArray) {
        NSMutableArray *array = [selectModel allHaoma];
        
        for (HaomaModel *haomaModel in array) {
            if (haomaModel.selected) {
                [selectArray addObject:haomaModel];
            }
        }
    }

    CGFloat money = [self.model payMoneyWithSelectedArray:selectArray];
    
    zhushuLabel.text = [NSString stringWithFormat:@"注数：%d",self.model.zhushu];
    moneyLabel.text  = [NSString stringWithFormat:@"金额：%.2f元",money];//投注数*倍数*2元
    
    if ([self.hDelegate respondsToSelector:@selector(payMoneyDidChanged)]) {
        [self.hDelegate payMoneyDidChanged];
    }
}


@end












